<?php

require_once 'classes/LightPressPlugin.php';

class PagedComments extends LightPressPlugin {
    
    // http://microformats.org/discuss/mail/microformats-rest/2006-April/000244.html
    
    var $constructor_args = array(
        'page_size'=>'number of comments to display on a page',
        'next'=>'string to use for the next page link',
        'previous'=>'string to use for the previous page link');
    var $default_context = 24; // LP_CONTEXT_POST | LP_CONTEXT_STATIC
    var $description = 'paginate post comments';
    
    var $active = true;
    var $hooks = array('get_comments', 'render_comments');
    
    var $page_size = 15;
    var $next = "next comments &raquo;";
    var $previous = "&laquo; previous comments";
    
    var $_num_comments;
    var $_start;
    
    function PagedComments(&$frontend, $args, $dummy_run=false) {
        $this->LightPressPlugin($frontend, $args, $dummy_run);
    }
    
    function run($hook, &$hook_data) {

        $context = $this->_frontend->context;
        $db =& $this->_frontend->db;
        $tpl =& $this->_frontend->tpl;
        $db->query("select count(*) as num from {$this->_frontend->tables['comments']} where comment_post_id={$hook_data['post_id']} and comment_approved='1'");
        $this->_num_comments = (int)$db->get('num');

        if ($hook == 'get_comments') {
            if ($context['index'] === 'last' && !is_null($hook_data['post_id'])) {
                if ($this->_num_comments > $this->page_size)
                    $hook_data['start'] = $context['index'] = $this->_num_comments - $this->page_size;
                else
                    $hook_data['start'] = $context['index'] = 0;
            } else {
                $hook_data['start'] = $context['index'];
            }
            $hook_data['num'] = $this->page_size;
            $this->_start = $hook_data['start'];
            return;
        }

        if ($this->_num_comments > 1 && ($this->_num_comments > $this->page_size)) {
            
            $tpl =& $this->_frontend->tpl;
            $tpl->setFile('plugin_pagedcomments', 'plugins/paged_comments.xml');
        
            $tpl_vars = array('plugin_paged_comments_num_comments'=>$this->_num_comments);
        
            $prefix = $this->_frontend->options['comments_prefix'];

            if ($context['index'] <= 0) {
                $tpl->setBlock('plugin_pagedcomments', 'plugin_paged_comments_prev');
                $tpl_vars['PLUGIN_PAGED_COMMENTS_PREV'] = '';
            } else {
                $tpl_vars['plugin_paged_comments_prev_url'] = "{$this->_frontend->options['url']}/{$hook_data['post_permalink']}/{$prefix}/" . ($context['index'] - 1) . '/#show_comments';
            }
            if (($this->_start * $this->page_size) < ($this->_num_comments - $this->page_size)) {
                $tpl->setBlock('plugin_pagedcomments', 'plugin_paged_comments_next');
                $tpl_vars['PLUGIN_PAGED_COMMENTS_NEXT'] = '';
            } else {
                $tpl_vars['plugin_paged_comments_next_url'] = "{$this->_frontend->options['url']}/{$hook_data['post_permalink']}/{$prefix}/" . ($context['index'] + 1) . '/#show_comments';
            }
            $start = $context['index'] * $this->page_size;
            $end = $start + $this->page_size;
            if ($end > $this->_num_comments)
                $end = $this->_num_comments;
            $tpl_vars['plugin_paged_comments_last_url'] = "{$this->_frontend->options['url']}/{$hook_data['post_permalink']}/{$prefix}/last/#show_comments";
            $tpl_vars['comments_list_start'] = $start + 1;
            $tpl->setVar($tpl_vars);
            $tpl->parse('PLUGIN_PAGEDCOMMENTS', 'plugin_pagedcomments');
        } else {
            $this->hide();
        }
        
    }
    
    function hide() {
        $tpl->setVar('comments_list_start', '1');
        $this->_frontend->tpl->setVar('PLUGIN_PAGEDCOMMENTS', '');
    }
    
}

?>